package com.mmy.controller.car;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.car.TCarType;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.car.impl.TCarTypeServiceImpl;

/**
 * 类的介绍：车型信息
 *
 * @author shangzl
 * @date 创建日期：2020-08-06
 * @version 1.0
 */
@Controller
public class CarTypeController {

	@Autowired
	TCarTypeServiceImpl tCarTypeServiceImpl;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期2020-08-06
	 */
	@RequestMapping(value = "/cartype")
	public String ToCarType(Model model) {
		try {

			List<TCarType> lstCarType = tCarTypeServiceImpl.findAll();
			model.addAttribute("cartypelist", lstCarType);

			return "webbackground/car/cartype";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/createcartype")
	public String ToCreateCarType() {
		return "webbackground/car/createcartype";
	}

	/**
	 * 函数功能说明： 新建车型信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期2020-08-06
	 */

	@RequestMapping(value = "/subcreatecartype")
	public String CreateCarType(TCarType tCarType, HttpSession session) throws Exception {
//		获取最大值
		Object oTypeID = tCarTypeServiceImpl.getMaxTypeID();

//		 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 
		if (oTypeID == null) {
			tCarType.setTypeID(1);

		} else {
			tCarType.setTypeID((int) oTypeID + 1);
		}
		String strTypeName = tCarType.getTypeName();
		try {
//			 判断名称是否为空 
			if (strTypeName == "" || strTypeName == null) {
				return "test";
			} else {
//				 判断名称是否已存在 
				if (tCarTypeServiceImpl.findByTypeName(strTypeName).size() > 0) {

					return "test";
				} else {
					tCarType.setCreateDate(new Date());
					TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
					tCarType.setCreator(tSysUser.getUserName());
					tCarTypeServiceImpl.save(tCarType.getTypeID());
					tCarTypeServiceImpl.save(tCarType);
					return "redirect:cartype";

				}
			}

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除车型信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */

	@PostMapping("/delcartype")
	@ResponseBody
	public String DelCarType(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstTypeID = new ArrayList<>();

		for (String str : checklist) {
			lstTypeID.add(Integer.parseInt(str));
		}

		tCarTypeServiceImpl.delCarType(lstTypeID);

		return "webbackground/car/cartype";
	}

	/**
	 * 函数功能说明： 修改车型信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */

	@RequestMapping(value = "/enterupdatecartype")
	public String EnterUpdateCarType(HttpServletRequest request, Model model) {

		int iTypeID = Integer.parseInt(request.getParameter("iTypeID"));

		TCarType tCarType = tCarTypeServiceImpl.findByTypeID(iTypeID);
		model.addAttribute("tCarType", tCarType);
		return "webbackground/car/updatecartype";

	}

	@RequestMapping(value = "/updatecartype")
	public String UpdateCarType(TCarType tCarType, HttpSession session) throws Exception {
		try {

//			 定义一个新的实体类对象 
			TCarType dtCarType = tCarTypeServiceImpl.findByTypeID(tCarType.getTypeID());
			dtCarType.setSeatNum(tCarType.getSeatNum());
			dtCarType.setTypeName(tCarType.getTypeName());
			dtCarType.setOilMass(tCarType.getOilMass());
			dtCarType.setMemo(tCarType.getMemo());
			dtCarType.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtCarType.setReviser(tSysUser.getUserName());
			tCarTypeServiceImpl.save(dtCarType);
			return "redirect:cartype";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询车型信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-06
	 */
	@RequestMapping(value = "/searchcartype")
	public String FindCarType(HttpServletRequest request, Model model) {

		String strTypeName = request.getParameter("TypeName");
		if (strTypeName == "") {
			List<TCarType> lstCarType = tCarTypeServiceImpl.findAll();
			model.addAttribute("cartypelist", lstCarType);
			request.setAttribute("strTypeName", strTypeName);
			return "webbackground/car/cartype";
		} else {
			List<TCarType> lstCarType = tCarTypeServiceImpl.findByTypeName(strTypeName);
			model.addAttribute("cartypelist", lstCarType);
			request.setAttribute("strTypeName", strTypeName);
			return "webbackground/car/cartype";
		}

	}

}
